<?php

$datum = explode(":", $mesiac);
$pocet_dni = date("t",mktime(0,0,0,$datum[0],1,$datum[1]));
echo '<table style="border-collapse: collapse; font-size: 10px; text-align: center; vertical-align: middle">';
$dni = array('ned','pon','uto','str','štv','pia','sob');
$prvyRiadok = '';
$druhyRiadok = '';
$medzera = '';
for ($i=1;$i<$pocet_dni+1;$i++) {
    if (jeSviatok(date("U",mktime(0,0,0,$datum[0],$i,$datum[1])))) {
        $prvyRiadok .= '<td style="background: yellow;border: 1px solid black; width: 40px; height: 15px; font-size: 10px;">';
        $druhyRiadok .= '<td style="background: yellow;border: 1px solid black; width: 40px; height: 15px; font-size: 10px;">';
    } else {
        $prvyRiadok .= '<td style="border: 1px solid black; width: 40px; height: 15px; font-size: 10px;">';
        $druhyRiadok .= '<td style="border: 1px solid black; width: 40px; height: 15px; font-size: 10px;">';
    }
    if ((date("w",mktime(0,0,0,$datum[0],$i,$datum[1]))==0)||(date("w",mktime(0,0,0,$datum[0],$i,$datum[1]))==6)) {
        $prvyRiadok .= '<b>';
        $prvyRiadok .= $dni[date("w",mktime(0,0,0,$datum[0],$i,$datum[1]))];
        $prvyRiadok .= '</b>';
        $druhyRiadok .= '<b>';
        $druhyRiadok .= $i.'.';
        $druhyRiadok .= '</b>';
    } else {
        $prvyRiadok .= $dni[date("w",mktime(0,0,0,$datum[0],$i,$datum[1]))];
        $druhyRiadok .= $i.'.';
    }
    $prvyRiadok .= '</td>';
    $druhyRiadok .= '</td>';
    $medzera .= '<td style="width: 40px; height: 10px;"></td>';
}
echo '<tr>';
echo '<th rowspan="2" style="width: 100px; border:1px solid black">Meno</th>';
echo $prvyRiadok;
echo '<th rowspan="2" style="width: 100px; border:1px solid black"># MOD</th>';
echo '<th rowspan="2" style="width: 100px; border:1px solid black"># Strava</th>';
echo '</tr>';
echo '<tr>';
echo $druhyRiadok;
echo '</tr>';
echo '<tr>';
echo $medzera;
echo '<th rowspan="1" style="width: 100px;"></th>';
echo '<th rowspan="1" style="width: 100px;"></th>';
echo '<th rowspan="1" style="width: 100px;"></th>';
echo '</tr>';

$zam_s = "SELECT * FROM z WHERE stavid=1 AND manager='1' ";
$zam_q = MySQL_Query ($zam_s);
while ($zam = MySQL_Fetch_Array($zam_q)) {
    echo '<tr>';
    echo '<th style="width: 100px; height: 20px; border:1px solid black">'.$zam['meno'].' '.$zam['priezvisko'].'</th>';
    for ($i=1;$i<$pocet_dni+1;$i++) {
        $nac_s = "SELECT * FROM dochadzkazamfalk WHERE zid = '".$zam['id']."' AND date='".(mktime(0,0,0,$datum[0],$i,$datum[1]))."' ";
        $nac_q = MySQL_Query ($nac_s);
        $nac = MySQL_Fetch_Array ($nac_q);
        if (!isset($nac['id'])) {
            if ((date("w",mktime(0,0,0,$datum[0],$i,$datum[1]))==0)||(date("w",mktime(0,0,0,$datum[0],$i,$datum[1]))==6)||(jeSviatok(date("U",mktime(0,0,0,$datum[0],$i,$datum[1]))))) {
                $value = 'OUT';
                $planOd = $planDo = $realOd = $realDo = '';
            } else {
                $value = 'IN';
                $planOd = $realOd = '00:00';
                $planDo = $realDo = '08:00';
            }
            if (jeSviatok(date("U",mktime(0,0,0,$datum[0],$i,$datum[1])))) {
                $sviatok = 1;
            } else {
                $sviatok = 0;
            }
            $uloz_i = "INSERT INTO dochadzkazamfalk (id, zid, date, type, planOd, planDo, realOd, realDo, sviatok) VALUES (NULL, '".$zam['id']."', '".(mktime(0,0,0,$datum[0],$i,$datum[1]))."', '".$value."', '".$planOd."', '".$planDo."', '".$realOd."', '".$realDo."', '".$sviatok."')";
            $uloz_q = MySQL_Query ($uloz_i);
        }
        ?>
<td style="border: 1px solid #cecece">
    <select size="1" name="type" style="height: 15px;font-size: 9px; width: 40px; padding: 0;" onchange="pridajDochadzku('<?php echo $zam['id'] ?>','<?php echo (mktime(0,0,0,$datum[0],$i,$datum[1])) ?>',this.value)">
        <option <?php if (typDochadzky($zam['id'], mktime(0,0,0,$datum[0],$i,$datum[1]))=='IN') echo 'selected="selected"' ?> value="IN" style="height: 15px;font-size: 10px;">IN</option>
        <option <?php if (typDochadzky($zam['id'], mktime(0,0,0,$datum[0],$i,$datum[1]))=='OUT') {
            echo 'selected="selected"';
        } elseif (((date("w",mktime(0,0,0,$datum[0],$i,$datum[1]))==0)||(date("w",mktime(0,0,0,$datum[0],$i,$datum[1]))==6)||(jeSviatok(date("U",mktime(0,0,0,$datum[0],$i,$datum[1])))))&&(typDochadzky($zam['id'], mktime(0,0,0,$datum[0],$i,$datum[1]))=='0')) {
            echo 'selected="selected"';
        } ?>value="OUT" style="height: 15px;font-size: 10px;">-</option>
        <option <?php if (typDochadzky($zam['id'], mktime(0,0,0,$datum[0],$i,$datum[1]))=='D') echo 'selected="selected"' ?> value="D" style="height: 15px;font-size: 10px;">D</option>
        <option <?php if (typDochadzky($zam['id'], mktime(0,0,0,$datum[0],$i,$datum[1]))=='LV') echo 'selected="selected"' ?> value="LV" style="height: 15px;font-size: 10px;">LV</option>
        <option <?php if (typDochadzky($zam['id'], mktime(0,0,0,$datum[0],$i,$datum[1]))=='PN') echo 'selected="selected"' ?> value="PN" style="height: 15px;font-size: 10px;">PN</option>
        <option <?php if (typDochadzky($zam['id'], mktime(0,0,0,$datum[0],$i,$datum[1]))=='DO') echo 'selected="selected"' ?> value="DO" style="height: 15px;font-size: 10px;">DO</option>
        <option <?php if (typDochadzky($zam['id'], mktime(0,0,0,$datum[0],$i,$datum[1]))=='MD') echo 'selected="selected"' ?> value="MD" style="height: 15px;font-size: 10px;">MD</option>
        <option <?php if (typDochadzky($zam['id'], mktime(0,0,0,$datum[0],$i,$datum[1]))=='MOD') echo 'selected="selected"' ?> value="MOD" style="height: 15px;font-size: 10px;">MOD</option>
    </select>
</td>
        <?php
    }
    echo '<th style="width: 100px; height: 20px; border:1px solid black" id="mod'.$zam['id'].'">';
    /*$vypis = 0;
    $nac_s = "SELECT kolko FROM falkmod WHERE zid = '".$zam['id']."' AND mesiac='".$datum[0]."' AND rok='".$datum[1]."'";
    $nac_q = MySQL_Query ($nac_s);
    $nac = MySQL_Fetch_Array ($nac_q);
    $vypis += $nac['kolko'];
    echo $vypis;*/
    echo vypocitatMOD($zam['id'],date("U",mktime(0,0,0,$datum[0],1,$datum[1])));
    echo '</th>';
    echo '<th style="width: 100px; height: 20px; border:1px solid black" id="strava'.$zam['id'].'">';
    /*$vypis = 0;
    $nac_s = "SELECT kolko FROM falkstrava WHERE zid = '".$zam['id']."' AND mesiac='".$datum[0]."' AND rok='".$datum[1]."'";
    $nac_q = MySQL_Query ($nac_s);
    $nac = MySQL_Fetch_Array ($nac_q);
    $vypis += $nac['kolko'];
    echo $vypis;*/
    echo vypocitatStravu($zam['id'],date("U",mktime(0,0,0,$datum[0],1,$datum[1])));
    echo '</th>';
    echo '</tr>';
}

echo '</table>';

?>